<template>
  <div id="register">
    <div class="title">会员注册</div>
    <div id="form">
      <van-form @submit="onSubmit">
        <van-field
          v-model="user.username"
          name="username"
          label="用户名"
          placeholder="用户名"
          :rules="[{ required: true, message: '请填写用户名' }]"
        />
        <van-field
          v-model="user.phone"
          name="phone"
          label="手机号"
          placeholder="手机号"
          :rules="[{ required: true, message: '请填写手机号' }]"
        />
        <van-field
          v-model="user.birthday"
          name="birthday"
          label="出生日期"
          placeholder="点击选择出生日期"
          :rules="[{ required: true, message: '请填写出生日期' }]"
          @click="showDatePicker = true"
        />
        <van-popup v-model="showDatePicker" position="bottom">
          <van-datetime-picker
            type="date"
            title="选择年月日"
            @confirm="onConfirm"
            @cancel="showPicker = false"
            :min-date="minDate"
            :max-date="maxDate"
          />
        </van-popup>
        <van-field
          v-model="user.age"
          name="age"
          label="年龄"
          placeholder="年龄"
          disabled
          :rules="[{ required: true, message: '请填写年龄' }]"
        />
        <van-field label="性别" name="sex">
          <template #input>
            <van-radio-group v-model="user.sex" direction="horizontal">
              <van-radio name="男">男</van-radio>
              <van-radio name="女">女</van-radio>
            </van-radio-group>
          </template>
        </van-field>
        <van-field
          v-model="user.home_address"
          name="home_address"
          label="家庭住址"
          placeholder="家庭住址"
          :rules="[{ required: true, message: '请填写家庭住址' }]"
        />
        <van-field
          v-model="user.shipping_address"
          name="shipping_address"
          label="收货地址"
          placeholder="收货地址"
          :rules="[{ required: true, message: '请填写收货地址' }]"
        />
        <van-field
          v-model="user.identity"
          name="identity"
          label="监护人"
          placeholder="监护人"
          :rules="[{ required: true, message: '请填写监护人' }]"
        />
        <div style="margin: 16px">
          <van-button round block type="info" native-type="submit"
            >注册</van-button
          >
        </div>
      </van-form>
    </div>
  </div>
</template>

<script>
import {
  Field,
  Button,
  Form,
  Popup,
  DatetimePicker,
  Radio,
  RadioGroup,
} from "vant";
import { post } from "../../request/index";
import axios from "axios";
const dayjs = require("dayjs");
export default {
  name: "Register",
  data() {
    return {
      user: {
        username: "",
        phone: "",
        birthday: undefined,
        age: "",
        sex: "",
        head_image_url: "",
        home_address: "",
        shipping_address: "",
        register_time: "",
        identity: "",
      },
      showDatePicker: false,
      minDate: new Date(1970, 1, 1),
      maxDate: new Date(),
    };
  },
  components: {
    [Field.name]: Field,
    [Button.name]: Button,
    [Form.name]: Form,
    [Popup.name]: Popup,
    [DatetimePicker.name]: DatetimePicker,
    [Radio.name]: Radio,
    [RadioGroup.name]: RadioGroup,
  },
  async created() {
    console.log("进入了注册会员页面");
    const { data } = await axios.get(
      "https://gc.frp.aiyouei.com/weChat/userInfo"
    );
    console.log(data);
  },
  methods: {
    async onSubmit(values) {
      values.birthday = values.birthday + " 00:00:00";
      values.sex = values.sex === "男" ? 1 : 0;
      const result = await post(`/api/user/save`, values);
      console.log(result);
      if (result.status === "OK") {
        this.$toast(result.message);
      }
    },
    onConfirm(date) {
      this.user.birthday = dayjs(date).format("YYYY年MM月DD日");
      this.user.age = dayjs(new Date()).diff(dayjs(date), "years");
      this.showDatePicker = false;
    },
  },
};
</script>

<style lang="less" scoped>
.title {
  width: 100%;
  height: 30px;
  line-height: 30px;
  text-align: center;
  font-size: 18px;
  font-weight: bold;
}
</style>